package zuoye_3_31;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


//从csv读取数据。把数据按照小时，30分钟，15分钟输出并打印。
public class Wenti_1 {
	public static void main(String[] args) throws Exception {
		ArrayList<String> listH = new ArrayList<>();
		ArrayList<String> list30 = new ArrayList<>();
		ArrayList<String> list15 = new ArrayList<>();

		BufferedReader reader = new BufferedReader(new FileReader("data.txt"));
		ArrayList listPrimary = new ArrayList<>();
		String line;
		while((line=reader.readLine())!=null) {
			listPrimary.add(reader.readLine());
		}
		System.out.println("以下是第一题:");
		//处理小时数据
		String str1 = (String) listPrimary.get(0);
		String[] arr1 = str1.split(";");
		for (String string : arr1) {
			listH.add(string);
		}
		System.out.println("小时:");
		printdata(listH);
		//System.out.println(listH);
		
		//处理30分钟数据
		String str2 = (String) listPrimary.get(1);
		String[] pairs = str2.split(";");
		for(String pair : pairs) {
		    // 使用","分割每一对数据
		    String[] segments = pair.split(",");
		    for(String segment : segments) {
		        list30.add(segment);
		    }
		}
		System.out.println("30分钟:");
		printdata(list30);
		//System.out.println(list30);
		
		//处理15分钟数据
		String str3 = (String) listPrimary.get(2);
		String[] pairs1 = str3.split(";");
		for(String pair : pairs1) {
		    // 使用","分割每一对数据
		    String[] segments = pair.split(",");
		    for(String segment : segments) {
		        list15.add(segment);
		    }
		}
		System.out.println("15分钟:");
		printdata(list15);
		
		
		System.out.println("=====================");
		System.out.println("以下是第二题:");
		//请按照小时+30分钟组装数据，进行转换，如把小时数据和小时对应的30分钟数据组装如下的Map，
//		如<0130，23>，<0160，34>，<0230，12>，<0260，15>，以此类推，
//		对结果进行打印，并对这个Map按照整数Value从大到小排序，返回排序后的Map，进行打印。
		HashMap<String,String> map30 = new HashMap<>();
		map30 = Assembly(listH, list30, 2);
		System.out.println("未排序的:");
		printmap(map30);
		System.out.println("排序过后的:");
		sortmap(map30);
		
		
		
		
		
		//以下是第三题
		System.out.println("============");
		System.out.println("以下是第三题:");
		
		HashMap<String,String> map15 = new HashMap<>();
		map15 = Assembly(listH,list15,4);
		System.out.println("未排序的:");
		printmap(map15);
		System.out.println("排序后的:");
		sortmap(map15);
		
	}

	
	
	
	private static void printmap(HashMap<String, String> map) {
		// TODO Auto-generated method stub
		for (Entry<String, String> entry : map.entrySet()) {
			String key = entry.getKey();
			String val = entry.getValue();
			System.out.print("<"+key+","+val+">"+" ");
		}
		System.out.println();
	}




	private static void sortmap(HashMap<String, String> map) {
		// TODO Auto-generated method stub
		//将HashMap转化为list再进行排序操作
				List<Map.Entry<String, String>> list = new ArrayList<>(map.entrySet());
				list.sort(new Comparator<Map.Entry<String, String>>() {
				    @Override
				    public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
				        return Integer.parseInt(o2.getValue()) - Integer.parseInt(o1.getValue());
				    }
				});
				for (Entry<String, String> entry : list) {
					System.out.print("<"+entry.getKey()+","+entry.getValue()+">"+" ");
				}
	}




	private static HashMap<String, String> Assembly(ArrayList<String> list1, ArrayList<String> list2, int limit) {
		// TODO Auto-generated method stub
		HashMap<String,String> map = new HashMap<>();
		ArrayList<String> listT = new ArrayList<String>();
		listT = (ArrayList<String>) list2.clone();
		int flag = 0;
		int num = 0;
		for (String sH : list1) {
			if(flag>=limit) {
				flag = 0;
				for (int i = 0; i < limit; i++) {
					listT.remove(0);
				}
			}
			for (String string : listT) {
				if(flag == limit) {
					break;
				}
				String[] sT1 = string.split(":");
				if(sT1[0].contains("~")){
					String[] sP2 = sT1[0].split("~");
					map.put(0+sH+sP2[1], sT1[1]);
				}else if(sT1[0].contains("-")) {
					String[] sP2 = sT1[0].split("-");
					map.put(0+sH+sP2[1], sT1[1]);
				}
				flag++;
			}
		}
		return map;
		
	}




	private static void printdata(ArrayList<String> list) {
		// TODO Auto-generated method stub
		int size = list.size();
		for (int i=0;i<size;i++) {
		    System.out.print(list.get(i));
		    if (i!=size-1) {
		        System.out.print("; ");
		    }
		}
		System.out.println();
	}
}
